﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NJT.星期
{
    public static class 星期转换
    {

        /// <summary>
        ///     The 星期显示组 如 1, "星期一", DayOfWeek.Monday,false
        /// </summary>
        public static 星期信息[] 星期显示组 =
            new 星期信息[] 
            {
                new 星期信息(0, "星期日", DayOfWeek.Sunday, false),
                new 星期信息(1, "星期一", DayOfWeek.Monday, false),
                new 星期信息(2, "星期二", DayOfWeek.Tuesday, false),
                new 星期信息(3, "星期三", DayOfWeek.Wednesday, false),
                new 星期信息(4, "星期四", DayOfWeek.Thursday, false),
                new 星期信息(5, "星期五", DayOfWeek.Friday, false),
                new 星期信息(6, "星期六", DayOfWeek.Saturday, false),
              
            };

        public static HashSet<Tuple<int, string>> 星期快捷数组 =
            new HashSet<Tuple<int, string>>
            {
                new Tuple<int, string>(0, "今天"),
                new Tuple<int, string>(1, "明天")
            };




        public static 星期信息 To星期信息(int k)
        {
            var k2 = 星期数字修正(k);
            return 星期显示组[k2];
        }
        /// <summary>
        /// 保证数字在0-6范围内.
        /// </summary>
        /// <param name="k">The k.</param>
        /// <returns>System.Int32.</returns>
        static int 星期数字修正(int k)
        {
            return (Math.Abs(k) + 7) % 7;
        }

        /// <summary>
        ///     如 1 转换为 "星期一"
        /// </summary>
        /// <param name="k">The k.</param>
        /// <returns>System.String.</returns>
        public static string ToWeek中文(int k)
        {
            var k2 = 星期数字修正(k);
            return 星期显示组.First(x => x.数字 == k2).中文;
        }
        public static string ToWeek中文(DayOfWeek k)
        {
            return System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(k);
        }
        /// <summary>
        ///     日期转为 "星期一"
        /// </summary>
        /// <param name="日期">The 日期.</param>
        /// <returns>System.String.</returns>
        public static string ToWeek中文(DateTime 日期)
        {
            var week = 日期.DayOfWeek;
            return 星期显示组.First(x => x.英文 == week).中文;
        }


        /// <summary>
        ///     DayOfWeek.Monday 转为1
        /// </summary>
        /// <param name="week">The week.</param>
        /// <returns>System.Int32.</returns>
        public static int ToWeekInt(DayOfWeek week)
        {
            return 星期显示组.First(x => x.英文 == week).数字;
        }
        /// <summary>
        ///     如 "星期一" 转换为 1
        /// </summary>
        /// <param name="k">The k.</param>
        /// <returns>System.Int32.</returns>
        public static int ToWeekInt(string k)
        {
            var r = 星期显示组.FirstOrDefault(x => x.中文 == k);
            return r == null ? 1 : r.数字;
        }

        /// <summary>
        ///     DayOfWeek.Monday 转为1
        /// </summary>
        /// <param name="日期">The 日期.</param>
        /// <returns>System.Int32.</returns>
        public static int ToWeekInt(DateTime 日期)
        {
            var week = 日期.DayOfWeek;
            return 星期显示组.First(x => x.英文 == week).数字;
        }

        /// <summary>
        ///     返回日期和今天的差值. 今天为0;明天为1
        /// </summary>
        /// <param name="日期">The 日期.</param>
        /// <returns>System.Int32.</returns>
        public static int ToInt差(DateTime 日期)
        {
            return (日期 - DateTime.Today).Days;
        }

        /// <summary>
        /// 如今天为周三,k=4,返回明天日期,k=2 返回下周二日期.k=3,返回今天日期
        /// </summary>
        /// <param name="k">The k.</param>
        /// <returns>DateTime.</returns>
        public static DateTime To日期(int k)
        {
            var k2 = ToWeekInt(DateTime.Today);
            var d = (k + 7 - k2) % 7;
            return DateTime.Today.AddDays(d);
        }



    }
}
